// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/functions/macros.h>
#include <pollux/functions/lib/tdigest.h>
#include <pollux/functions/prestosql/types/tdigest_type.h>

namespace kumo::pollux::functions {
    template<typename T>
    struct ValueAtQuantileFunction {
        POLLUX_DEFINE_FUNCTION_TYPES(T);
        MELON_ALWAYS_INLINE void call(
            out_type<double> &result,
            const arg_type<SimpleTDigest<double> > &input,
            const arg_type<double> &quantile) {
            TDigest<> digest;
            std::vector<int16_t> positions;
            digest.mergeDeserialized(positions, input.data());
            digest.compress(positions);
            result = digest.estimateQuantile(quantile);
        }
    };

    template<typename T>
    struct ValuesAtQuantilesFunction {
        POLLUX_DEFINE_FUNCTION_TYPES(T);

        MELON_ALWAYS_INLINE void call(
            out_type<Array<double> > &result,
            const arg_type<SimpleTDigest<double> > &input,
            const arg_type<Array<double> > &quantiles) {
            TDigest<> digest;
            std::vector<int16_t> positions;
            digest.mergeDeserialized(positions, input.data());
            digest.compress(positions);
            result.resize(quantiles.size());
            for (size_t i = 0; i < quantiles.size(); ++i) {
                result[i] = digest.estimateQuantile(quantiles[i].value());
            }
        }
    };

    template<typename T>
    struct MergeTDigestFunction {
        POLLUX_DEFINE_FUNCTION_TYPES(T);
        MELON_ALWAYS_INLINE bool call(
            out_type<SimpleTDigest<double> > &result,
            const arg_type<Array<SimpleTDigest<double> > > &input) {
            TDigest<> digest;
            std::vector<int16_t> positions;
            bool hasValidInput = false;
            for (auto i = 0; i < input.size(); i++) {
                if (!input[i].has_value()) {
                    continue;
                }
                hasValidInput = true;
                const auto &tdigest = input[i].value();
                digest.mergeDeserialized(positions, tdigest.data());
            }
            if (!hasValidInput) {
                return false;
            }
            digest.compress(positions);
            int64_t size = digest.serializedByteSize();
            result.resize(size);
            digest.serialize(result.data());
            return true;
        }
    };
} // namespace kumo::pollux::functions
